package util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

import static org.codehaus.groovy.runtime.IOGroovyMethods.getBytes;

/**
 * Created by sola on 2017/11/14.
 * 微信dat文件转pic 异或
 * 若a^b=c
 * 则a=b^c
 */

public class DatToPic {
    //需要计算
    private static int magic = 0x8B;
    static String[] allowExt = new String[]{"", "dat"};

    public static void decodeWechat(File dict) throws IOException {
        if (dict.isFile()) {
            String fullName, ext = null;
            fullName = dict.getName();
            ext = getFileExt(fullName);
            File parentFile = new File(dict.getParentFile() + "copy");
            if (!parentFile.exists()) {
                parentFile.mkdir();
            }
            if (Arrays.asList(allowExt).contains(ext)) {

                File outFile = new File(dict.getParentFile() + "copy", fullName + "_new.jpg");
                byte[] byArr = getBytes(new FileInputStream(dict));
                for (int i = 0, len = byArr.length; i < len; i++) {
                    byArr[i] = (byte) (byArr[i] ^ magic);
                }
                FileOutputStream output = new FileOutputStream(outFile);
                output.write(byArr);
                output.flush();
                output.close();
                //IOUtils.write(byArr, output);
                //.closeQuietly(output);
            }
        } else {
            File[] fileArr = dict.listFiles();
            for (int i = 0, len = fileArr.length; i < len; i++) {
                decodeWechat(fileArr[i]);
            }
        }
    }

    public static String getFileExt(String fileName) {
        if (fileName == null) {
            return "";
        } else {
            int i = fileName.lastIndexOf(".");
            if (i < 0) {
                return "";
            } else {
                return fileName.substring(i + 1).toLowerCase();
            }
        }
    }

    public static void main(String[] args) throws IOException {
        //byte[] a= new byte("02db");
        byte[] magic =new byte[10];
        magic[0]= (byte) (0xff ^ 0xffd8);
        File file = new File("C:/Users/Arat.Wu/Documents/WeChat Files/wxid_j7rulepnqzx121/Data/Favorite/thumb");
        decodeWechat(file);
    }
        /*
            //------------------------------------------------
            //--- 010 Editor v8.0.1 Script File
            //
            //      File:
            //   Authors:
            //   Version:
            //   Purpose:
            //  Category:
            //   History:
            //------------------------------------------------
            RequiresVersion( 4.0 );
            RequiresFile();
            const int BLOCK_SIZE = 1024;
            uchar buffer[ BLOCK_SIZE ];
            quad size, pos;
            int i, bufsize; 
            pos = 0;
            size = FileSize();
            while( size > 0 )
            {
                bufsize = size < BLOCK_SIZE ? size : BLOCK_SIZE>
                ReadBytes( buffer, pos, bufsize );
                for( i = 0; i < bufsize; i++)
                {
                    buffer[i] = buffer[i] ^ 0x8b; }
                WriteBytes( buffer, pos, bufsize );
                pos += bufsize;
                size -= bufsize;
            }
             return 1; 
         */



}